<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>收款收据模版</title>
    <style>
        body {
            font-family: 'SimSun', '宋体', sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #fff;
            display: flex;
            justify-content: center;
        }
        .container {
            background-color: white;
            padding: 30px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            position: relative;
            margin: 20px;
            max-width: 1200px;
            border-radius: 8px;
        }
        .receipt {
            border: 2px solid #333;
            position: relative;
            margin: 20px;
            padding: 10px;
            background-color: white;
            transition: all 0.3s ease;
        }
        /* 背景颜色选项 */
        .receipt.bg-white {
            background-color: white;
        }
        .receipt.bg-blue {
            background-color: #e6f4ff;
        }
        .receipt.bg-green {
            background-color: #e8f5e9;
        }
        .receipt.bg-yellow {
            background-color: #fff9e6;
        }
        .receipt.bg-gray {
            background-color: #f5f5f5;
        }
        .receipt-header {
            text-align: center;
            margin-bottom: 20px;
        }
        .receipt-title {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }
        .receipt-info {
            display: flex;
            justify-content: space-between;
            margin-bottom: 15px;
            font-size: 16px;
        }
        .receipt-items {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        .receipt-items th, .receipt-items td {
            border: 1px solid #333;
            padding: 8px;
            text-align: center;
        }
        .receipt-items th {
            font-weight: bold;
        }
        .amount-cell {
            text-align: right;
        }
        .receipt-footer {
            text-align: right;
            margin-top: 20px;
            font-size: 16px;
        }
        .controls-container {
            position: fixed;
            right: 20px;
            top: 50%;
            transform: translateY(-50%);
            z-index: 100;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }
        .print-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 10px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .print-btn:hover {
            background: linear-gradient(135deg, #5a6fc5 0%, #6a3e91 100%);
            transform: translateY(-2px);
            box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
        }
        .print-btn:active {
            transform: translateY(0);
        }
        .editable {
            border-bottom: 1px dotted #666;
            width: 250px;
            display: inline-block;
            padding: 2px 5px;
            text-align: left;
            transition: all 0.2s ease;
        }
        .editable:focus {
            outline: none;
            border-bottom: 2px solid #2196F3;
            background-color: #f9f9f9;
        }
        .amount-display {
            min-width: 150px;
            display: inline-block;
            text-align: right;
        }
        .signature-fields {
            display: flex;
            justify-content: space-between;
            margin-top: 30px;
            font-size: 16px;
        }
        .signature-fields > div {
            flex: 1;
            margin: 0 10px;
            text-align: center;
        }
        .date-input {
            display: inline-block;
            text-align: center;
            border: none;
            border-bottom: 1px dotted #333;
            margin: 0 5px;
            transition: all 0.2s ease;
            background: transparent;
        }
        .date-input:focus {
            outline: none;
            border-bottom: 2px solid #2196F3;
            background-color: #f9f9f9;
        }
        .watermark {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 80px;
            color: rgba(200, 200, 200, 0.2);
            z-index: 0;
            pointer-events: none;
            transform: translate(-50%, -50%) rotate(-15deg);
        }
        /* 背景选择器样式 */
        .bg-selector {
            background: white;
            padding: 15px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
        }
        .bg-selector h3 {
            margin-top: 0;
            margin-bottom: 10px;
            font-size: 16px;
            color: #333;
            border-bottom: 1px solid #eee;
            padding-bottom: 5px;
        }
        .bg-option {
            display: block;
            margin: 5px 0;
            padding: 8px 10px;
            border-radius: 3px;
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
        }
        .bg-option:hover {
            background: #f0f0f0;
        }
        .bg-option.active {
            background: #e0e0ff;
            font-weight: bold;
            color: #333;
        }
        .bg-option .color-preview {
            width: 16px;
            height: 16px;
            border-radius: 50%;
            margin-right: 8px;
            display: inline-block;
            border: 1px solid #ddd;
        }
        /* 打印样式 */
        @media print {
            body {
                background-color: white;
                padding: 0;
            }
            .container {
                max-width: none;
                box-shadow: none;
                padding: 0;
            }
            .receipt {
                margin-top: 0;
                border: 2px solid #000;
                padding: 20px;
            }
            .controls-container, .watermark {
                display: none !important;
            }
            .editable {
                border-bottom: 1px dotted #000;
            }
            .date-input {
                border-bottom: 1px dotted #000;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div id="receipt" class="receipt bg-white">
            <div class="watermark">收款收据</div>
            <div class="receipt-header">
                <div class="receipt-title">收款收据</div>
            </div>
            
            <div class="receipt-info" style="position: absolute;left: 10px;top: 10px;">
                <div><span id="receiptNo" class="editable" contenteditable="true" style="width:120px;"></span></div>
            </div>
            <div class="receipt-info">
                <div>付款单位: <span id="payer" class="editable" contenteditable="true"></span></div>
                <div>付款方式: <span id="paymentMethod" class="editable" contenteditable="true" style="width:50px;"></span></div>
            </div>
            
            <div class="receipt-info">
                <div>收款单位: <span id="receiver" class="editable" contenteditable="true"></span></div>
                <div>付款日期: 
                    <input type="text" id="yearInput" class="date-input" placeholder="年" maxlength="4" style="width:40px;">年
                    <input type="text" id="monthInput" class="date-input" placeholder="月" maxlength="2" style="width:20px;">月
                    <input type="text" id="dayInput" class="date-input" placeholder="日" maxlength="2" style="width:20px;">日
                </div>
            </div>
            <table class="receipt-items">
                <thead>
                    <tr>
                        <th>收款事由</th>
                        <th class="amount-cell">金额（元）</th>
                    </tr>
                </thead>
                <tbody id="itemsTable">
                    <tr>
                        <td><span class="editable item-name" contenteditable="true" style="width:300px;"></span></td>
                        <td class="amount-cell"><span class="editable amount-input" contenteditable="true" style="text-align:center;">0.00</span></td>
                    </tr>
                </tbody>
            </table>
            
            <div class="receipt-info">
                <div>人民币（大写）: <span id="amountInWords" class="amount-display">零元整</span></div>
                <div style="width: 40px;margin-right: 100px;">盖章单位</div>
            </div>
            
            <div class="signature-fields">
                <div>
                    <p>记账：<span class="editable" contenteditable="true" style="width: 80px;text-align: center;"></span></p>
                </div>
                <div>
                    <p>出纳：<span class="editable" contenteditable="true" style="width: 80px;text-align: center;"></span></p>
                </div>
                <div>
                    <p>审核：<span class="editable" contenteditable="true" style="width: 80px;text-align: center;"></span></p>
                </div>
                <div>
                    <p>经办：<span class="editable" contenteditable="true" style="width: 80px;text-align: center;"></span></p>
                </div>
            </div>
        </div>
        
        <!-- 控制面板 -->
        <div class="controls-container">
            <!-- 背景选择器 -->
            <div class="bg-selector">
                <h3>选择背景</h3>
                <div class="bg-option active" data-bg="bg-white">
                    <span class="color-preview" style="background-color: white;"></span>
                    纯白色
                </div>
                <div class="bg-option" data-bg="bg-blue">
                    <span class="color-preview" style="background-color: #e6f4ff;"></span>
                    淡蓝色
                </div>
                <div class="bg-option" data-bg="bg-green">
                    <span class="color-preview" style="background-color: #e8f5e9;"></span>
                    淡绿色
                </div>
                <div class="bg-option" data-bg="bg-yellow">
                    <span class="color-preview" style="background-color: #fff9e6;"></span>
                    淡黄色
                </div>
                <div class="bg-option" data-bg="bg-gray">
                    <span class="color-preview" style="background-color: #f5f5f5;"></span>
                    淡灰色
                </div>
            </div>
            
            <!-- 打印按钮 -->
            <button type="button" id="printReceipt" class="print-btn">
                <i class="fa fa-print mr-2"></i>打印收据
            </button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 设置默认日期
            const now = new Date();
            document.getElementById('yearInput').value = now.getFullYear();
            document.getElementById('monthInput').value = now.getMonth() + 1;
            document.getElementById('dayInput').value = now.getDate();
            
            // 生成唯一编号
            generateReceiptNo();
            
            // 打印收据
            document.getElementById('printReceipt').addEventListener('click', function() {
                window.print();
            });
            
            // 监听所有金额输入变化
            document.querySelectorAll('.amount-input').forEach(input => {
                input.addEventListener('input', updateTotalAmount);
            });
            
            // 更新总计金额和大写金额
            function updateTotalAmount() {
                let total = 0;
                document.querySelectorAll('.amount-input').forEach(input => {
                    let value = input.textContent.replace(/[^\d.-]/g, '');
                    value = parseFloat(value) || 0;
                    input.textContent = value.toFixed(2);
                    total += value;
                });
                document.getElementById('amountInWords').textContent = numberToChinese(total);
            }
            
            // 数字转中文大写（优化版）
            function numberToChinese(num) {
                if (isNaN(num) || num === 0) return '零元整';
                
                const digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
                const units = ['', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '兆'];
                
                // 处理整数部分
                let str = '';
                const numStr = Math.floor(num).toString();
                const len = numStr.length;
                
                for (let i = 0; i < len; i++) {
                    const digit = parseInt(numStr[i]);
                    const pos = len - i - 1;
                    
                    if (digit !== 0) {
                        str += digits[digit] + units[pos];
                    } else {
                        // 当前位是0，判断是否需要补零
                        if (pos === 4 || pos === 8) {
                            // 万、亿位的0需要补单位
                            str += units[pos];
                        }
                        
                        // 下一位非零则补零
                        if (i < len - 1 && parseInt(numStr[i + 1]) !== 0) {
                            str += digits[0];
                        }
                    }
                }
                
                str += '元';
                
                // 处理小数部分（角分）
                const decimal = Math.round((num - Math.floor(num)) * 100);
                const jiao = Math.floor(decimal / 10);
                const fen = decimal % 10;
                
                if (jiao > 0) {
                    str += digits[jiao] + '角';
                }
                
                if (fen > 0) {
                    str += digits[fen] + '分';
                } else if (jiao === 0) {
                    str += '整';
                }
                
                return str;
            }
            
            // 生成收据编号
            function generateReceiptNo() {
                const year = document.getElementById('yearInput').value;
                const month = document.getElementById('monthInput').value.padStart(2, '0');
                const day = document.getElementById('dayInput').value.padStart(2, '0');
                const random = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
                
                const receiptNo = `NO.${year}${month}${day}${random}`;
                document.getElementById('receiptNo').textContent = receiptNo;
            }
            
            // 监听日期变化，更新收据编号
            document.getElementById('yearInput').addEventListener('change', generateReceiptNo);
            document.getElementById('monthInput').addEventListener('change', generateReceiptNo);
            document.getElementById('dayInput').addEventListener('change', generateReceiptNo);
            
            // 自动计算所有可编辑字段的变化
            document.querySelectorAll('[contenteditable="true"]').forEach(el => {
                el.addEventListener('input', function() {
                    if (el.classList.contains('amount-input')) return; // 已单独处理
                });
            });
            
            // 日期输入限制
            document.querySelectorAll('.date-input').forEach(input => {
                input.addEventListener('input', function() {
                    this.value = this.value.replace(/[^0-9]/g, '');
                    
                    // 年、月、日长度限制
                    if (this.id === 'yearInput' && this.value.length > 4) {
                        this.value = this.value.slice(0, 4);
                    } else if ((this.id === 'monthInput' || this.id === 'dayInput') && this.value.length > 2) {
                        this.value = this.value.slice(0, 2);
                    }
                    
                    // 月份范围限制
                    if (this.id === 'monthInput' && parseInt(this.value) > 12) {
                        this.value = '12';
                    }
                    
                    // 日期范围限制
                    if (this.id === 'dayInput' && parseInt(this.value) > 31) {
                        this.value = '31';
                    }
                });
            });
            
            // 背景选择功能
            const bgOptions = document.querySelectorAll('.bg-option');
            const receipt = document.getElementById('receipt');
            
            bgOptions.forEach(option => {
                option.addEventListener('click', function() {
                    // 移除所有active类
                    bgOptions.forEach(opt => opt.classList.remove('active'));
                    // 添加当前active类
                    this.classList.add('active');
                    // 更新收据背景
                    const bgClass = this.getAttribute('data-bg');
                    // 移除所有背景类
                    receipt.className = 'receipt';
                    // 添加选中的背景类
                    receipt.classList.add(bgClass);
                });
            });
        });
    </script>
</body>
</html>